<?php

class Admin_m extends CI_Model {

    function __construct() {
        parent::__construct();
        $this->load->database();
    }

    function list_content($page) {
        return $this->db->get_where('tblContent', array('page' => $page))->result_array();
    }

    function get_content_details($page, $id) {
        return $this->db->get_where('tblContent', array('page' => $page, 'id' => $id))->row();
    }

    function update_page($page, $data, $id) {
        $query = $this->db->update('tblContent', $data, array('id' => $id));
        if ($this->db->affected_rows() > 0) {
            return 1;
        } else {
            return 0;
        }
    }

    public function login($email, $password) {
        if ($email == null && $password == null) {
            return FALSE;
        }

        $query = $this->db->select('id, group_id, email, password')
                ->from('users')
                ->where(array('email' => $email, 'password' => $password))
                ->get();
        if ($query->num_rows() > 0) {
            $userRows = $query->row();

            $this->session->set_userdata("user_id", $userRows->id);
            $this->session->set_userdata("group_id", $userRows->group_id);

            //update last login time

            return TRUE;
        } else {
            return FALSE;
        }
    }
    
    function change_pass($user_id, $password) {
        return $this->db->update('users', array('password' => $password), array('id' => $user_id));
    }
    
    function is_password($old_pass, $user_id) {
        return $this->db->where(array('password' => $old_pass, 'id' => $user_id))
                ->count_all_results('users');
    }
    
    function logout() {
       /* $this->session->unset_userdata('email');
        $this->session->unset_userdata('id');*/
        $this->session->unset_userdata('user_id');
        $this->session->unset_userdata('group_id');
    }

}

?>
